﻿using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApiExtend
{
    /// <summary>
    /// 日志帮助类
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// 
        /// </summary>
        private NLog.Logger logger;

        /// <summary>
        /// NLog日志
        /// </summary>
        public LogHelper()
        {
            this.logger = LogManager.GetCurrentClassLogger();
        }

        /// <summary>
        /// debugging information, 比 Trance 级别稍微粗略，一般在生产环境中不启用。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="folderName"></param>
        public async Task Debug<T>(T t, string folderName = "Debug")
        {
            await Task.Run(() =>
            {
                logger.Properties["folderName"] = folderName;
                logger.Debug(t);
            });
        }

        /// <summary>
        /// information messages，一般在生产环境中启用。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="folderName"></param>
        public async Task Info<T>(T t, string folderName = "Info")
        {
            await Task.Run(() =>
            {
                logger.Properties["folderName"] = folderName;
                logger.Info(t);
            });
        }

        /// <summary>
        /// warning messages，一般用于可恢复或临时性错误的非关键问题。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="folderName"></param>
        public async Task Warn<T>(T t, string folderName = "Warn")
        {
            await Task.Run(() =>
            {
                logger.Properties["folderName"] = folderName;
                logger.Warn(t);
            });
        }

        /// <summary>
        /// error messages，一般是异常信息。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="folderName"></param>
        public async Task Error<T>(T t, string folderName = "Error")
        {
            await Task.Run(() =>
            {
                logger.Properties["folderName"] = folderName;
                logger.Error(t);
            });
        }

        /// <summary>
        /// 非常严重的错误！
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="folderName"></param>
        public async Task Fatal<T>(T t, string folderName = "Fatal")
        {
            await Task.Run(() =>
            {
                logger.Properties["folderName"] = folderName;
                logger.Error(t);
            });
        }
    }
}
